<?php
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Front Controller plug in to set up the view with the Wrp view helper
 * path and some useful request variables.
 *
 */
class My_Controller_Plugin_ViewSetup extends Zend_Controller_Plugin_Abstract
{    
    /**
     * @var Zend_View
     */
    protected $_view;
    
    public function predispatch(Zend_Controller_Request_Abstract $request)
    {
       $request = Zend_Controller_Front::getInstance()->getRequest(); 
       $params = $request->getParams();
       $url = $params['module'].'/'.$params['controller'].'/'.$params['action'];
       unset($params['module']);
       unset($params['controller']);
       unset($params['action']);
       foreach($params as $k=>$v):
           $url.= '/'.$k.'/'.$v;
       endforeach;
        
        $result = preg_match('/(ajax)/i', $url,$matches);
        if(!$result){
            $session = new Zend_Session_Namespace('history'); 
            if($url!=$session->url):
                $session->referrer = (string)$session->url;
                $session->url = (string)$url;
            endif;
        } 
    }
    
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->init();
        
        $view = $viewRenderer->view;
        $this->_view = $view;
        $view->setEncoding('UTF-8');
        
        // set up common variables for the view
        $view->originalModule = $request->getModuleName();
        $view->originalController = $request->getControllerName();
        $view->originalAction = $request->getActionName();

     
        // add helper path to View/Helper directory within this library
        $prefix = 'My_View_Helper';
        $dir = dirname(__FILE__) . '/../../View/Helper';
        $view->addHelperPath($dir, $prefix);
        
        // set up doctype for any view helpers that use it
        $view->setEncoding('UTF-8');
        $view->doctype('XHTML1_STRICT');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=UTF-8');
//        $view->headLink(array('rel' => 'shortcut icon','href' => $view->baseUrl() . '/images/prova32.ico',
//            'mce_href' => $view->baseUrl() . '/images/prova32.ico'),'PREPEND');

        ZendX_JQuery::enableView($view);
        $view->jQuery()->setLocalPath($view->baseUrl() . "/js/jquery-1.7.1.min.js")
                ->setUiLocalPath($view->baseUrl() . "/js/jquery-ui-1.8.12.custom.min.js")
                ->addStylesheet($view->baseUrl() . '/css/redmond/jquery-ui-1.8.18.custom.css')
                ->addJavascriptFile($view->baseUrl() . '/js/jquery.ui.datepicker-pt-BR.js');
        $view->jQuery()->enable();
        $view->jQuery()->uiEnable();

        $view->headLink()->appendStylesheet($view->baseUrl() . '/css/bootstrap.css');
        $view->headLink()->appendStylesheet($view->baseUrl() . '/css/padrao.css');
        $view->headLink()->appendStylesheet($view->baseUrl() . '/css/site.css');
        $view->headLink()->appendStylesheet($view->baseUrl() . '/css/menu_style.css');
        $view->headLink()->appendStylesheet($view->baseUrl() . '/css/boxes.css');

        $view->headScript()->appendFile($view->baseUrl().'/js/bootstrap.min.js');
        $view->headScript()->appendFile($view->baseUrl().'/js/bootstrap-tooltip.js');
////        $view->headScript()->appendFile($view->baseUrl().'/js/index.js');

    }
    
    
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        if (!$request->isDispatched()) {
            return;
        }
        $view = $this->_view;
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('GESAC');
        
    }
    
}